/**
 * 
 */
package com.leoj.utils.finite_states_machine;

import com.leoj.utils.CallbackMethod;
import com.leoj.utils.Couple;
import com.leoj.utils.Handler;



/**
 * @author leoj
 *
 */
public class Machine<State extends Enum<?>, Transition> implements Handler<Transition>{
	
	private State _state;
	private Handler<State> _actions;
	private CallbackMethod<State, Couple<State, Transition>> _transitionHandler;
	
	/**
	 * 
	 */
	public Machine(State initState, Handler<State> actions, CallbackMethod<State, Couple<State, Transition>> transitionHandler) {
		_state = initState;
		_actions = actions;
		_transitionHandler = transitionHandler;
	}
	public Machine(State initState, CallbackMethod<State, Couple<State, Transition>> transitions){
		this(initState, new Handler<State>() {
			@Override
			public void handle(State args) {
			}
		}, transitions);
	}
	/* (non-Javadoc)
	 * @see com.leoj.utils.Handler#handle(java.lang.Object)
	 */
	@Override
	public void handle(Transition transition) {
		_state = _transitionHandler.handle(new Couple<State, Transition>(_state, transition));
		_actions.handle(_state);
	}
	
	public State getCurrentState(){ return _state; }
	
}
